import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:photo_view/photo_view.dart';
import 'package:photo_view/photo_view_gallery.dart';
import 'package:yt_devops/configs.dart';
typedef PageChanged = void Function(int index);

class PhotoPreview extends StatefulWidget {
  final List<String> galleryItems; //图片列表
  final int defaultImage; //默认第几张
  final PageChanged? pageChanged; //切换图片回调
  final Axis direction; //图片查看方向

  PhotoPreview(this.galleryItems,
      {super.key, this.defaultImage = 1,
      this.pageChanged,
      this.direction = Axis.horizontal,})
      : assert(galleryItems.isNotEmpty);

  @override
  _PhotoPreviewState createState() => _PhotoPreviewState();
}

class _PhotoPreviewState extends State<PhotoPreview> {
  late int tempSelect;

  @override
  void initState() {
    super.initState();
    // TODO: implement initState
    tempSelect = widget.defaultImage + 1;
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Stack(
        children: [
          PhotoViewGallery.builder(
              scrollPhysics: const BouncingScrollPhysics(),
              builder: (BuildContext context, int index) {
                return PhotoViewGalleryPageOptions(
                    minScale: PhotoViewComputedScale.contained * 0.6,
                    maxScale: PhotoViewComputedScale.covered * 2,
                    initialScale:PhotoViewComputedScale.contained,
                  imageProvider: NetworkImage(Configs.enviromentModel.api +widget.galleryItems[index]),
                  onTapDown: (context, details, controllerValue){
                    Navigator.pop(context);
                  }
                );
              },
              scrollDirection: widget.direction,
              itemCount: widget.galleryItems.length,
              backgroundDecoration: const BoxDecoration(color: Colors.white),
              pageController: PageController(initialPage: widget.defaultImage),
              onPageChanged: (index) => setState(() {
                    tempSelect = index + 1;
                    if (widget.pageChanged != null) {
                      widget.pageChanged!(index);
                    }
                  })),
          Positioned(

              ///布局自己换
              right: 20,
              top: 620,
              child: Container(
                width: ScreenUtil().screenWidth,
                alignment: Alignment.center,
                child: Text(
                  '$tempSelect/${widget.galleryItems.length}',
                  style: const TextStyle(color: Colors.black),
                ),
              )),
          Positioned(
              child: Container(
                margin: EdgeInsets.only(top: ScreenUtil().statusBarHeight),
            child: IconButton(
              onPressed: () {
                Navigator.pop(context);
              },
              icon: const Icon(Icons.close),
            ),
          ))
        ],
      ),
    );
  }
}
